package cn.innosmart.aq.manager;

import com.tutk.p2p.ConnectionEntity;

import org.json.JSONException;
import org.json.JSONObject;

import cn.innosmart.aq.application.AQApplication;
import cn.innosmart.aq.util.SystemConstant;

/**
 * Created by David on 2015/7/1.
 */
public class NotifyManager extends BaseManager {
    private String Tag = "NotifyManager";


    private static class NotifyManagerInstance {
        private static final NotifyManager instance = new NotifyManager();
    }

    public static NotifyManager getInstance() {
        return NotifyManagerInstance.instance;
    }

    private NotifyManager() {
    }
    // NotifyManager.DebugOpen
    public boolean DebugOpen(ConnectionEntity connectionEntity) {
        JSONObject jData = new JSONObject();
        try {
            jData.put("debug", "on");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return SendNotify(connectionEntity, "palals.DebugInfoToggle", jData.toString(),
                "event/palals.DebugInfoToggle");
    }
    // NotifyManager.DebugClose
    public boolean DebugClose(ConnectionEntity connectionEntity) {
        JSONObject jData = new JSONObject();
        try {
            jData.put("debug", "off");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return SendNotify(connectionEntity, "palals.DebugInfoToggle", jData.toString(),
                "event/palals.DebugInfoToggle");
    }

    public void sendLoginSuccessEvent(ConnectionEntity connectionEntity) {
        try {
            // 构造指令
            JSONObject jRoot = new JSONObject();
            jRoot.put("type", "notif");
            jRoot.put("name", "connection.connected");
            JSONObject jData = new JSONObject();
            jData.put("username", connectionEntity.getUserName());
            jData.put("password", connectionEntity.getPassword());
            jData.put("clientid", AQApplication.getInstance().getMqttClientId());
            jRoot.put("data", jData);
            // 发送指令
            String msg = jRoot.toString();
            connectionEntity.sendNotify(SystemConstant.EVENT_TOPIC_APP_CONNECTION_SUCCESS, msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
